package com.lanou.web.controller;

import com.lanou.service.ContentCategoryService;
import com.lanou.utils.ShiHuiResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * Created by QuChao on 2018/11/23
 */
@RestController
@RequestMapping("/content/category")
public class ContentCategoryController {
    @Autowired
    private ContentCategoryService contentCategoryService;

    @RequestMapping("/list")
    public List<Map<String, Object>> list() {
        List<Map<String, Object>> list = contentCategoryService.list();
        return list;
    }
    /*
     * 添加类目
     * */
    @PostMapping("/create")
    public Map<String, Object> addCatrgory(Long parentId, String name) {
        List<Map<String, Object>> list = contentCategoryService.addCategory(parentId, name);
        return list != null ? ShiHuiResult.ok(list) : ShiHuiResult.error(400);
    }

    @PostMapping("/update")//更新
    public Map<String, Object> update(Long id, String name) {
        boolean result = contentCategoryService.updateById(id, name);
        return result ? ShiHuiResult.ok() : ShiHuiResult.error(400);
    }

    @PostMapping("/delete/")//删除
    public void deleteById(Long id) {
        contentCategoryService.deleteById(id);
    }
}
